package com.mandao;

import com.mandao.config.ProviderServiceConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author snail
 * ribbon-静态服务列表
 */
@RestController
@SpringBootApplication
@RibbonClient(name="provider-service" , configuration = ProviderServiceConfiguration.class)
public class ConsumerServiceApplication {
    @Bean
    @LoadBalanced
    RestTemplate restTemplate(){
        return new RestTemplate() ;
    }
    @Autowired
    private RestTemplate restTemplate ;

    /**
     * 直连方式
     * @param name String
     * @return String
     */
    @RequestMapping("/hi")
    public String hi(@RequestParam(value = "name", defaultValue = "springcloud") String name) {
        String greeting = this.restTemplate.getForObject("http://localhost:5555/greeting", String.class);
        return String.format("%s , %s!", greeting, name);
    }

    /**
     * ribbon
     * @param name String
     * @return String
     */
    @RequestMapping("/hello")
    public String hello(@RequestParam(value = "name", defaultValue = "springcloud") String name) {
        String greeting = this.restTemplate.getForObject("http://provider-service/greeting", String.class);
        return String.format("%s , %s!", greeting, name);
    }

    public static void main(String[] args) {
        SpringApplication.run(ConsumerServiceApplication.class, args);
    }

}
